home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src / context.c < prev    next >
C/C++ Source or Header  |  1999-02-04  |  60KB  |  2,127 lines

  1. /* $Id: context.c,v 3.21 1998/08/23 22:19:30 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: context.c,v $
  26.  * Revision 3.21  1998/08/23 22:19:30  brianp
  27.  * added DoViewportMapping to context struct
  28.  *
  29.  * Revision 3.20  1998/08/21 01:49:46  brianp
  30.  * initialize vertex array state
  31.  *
  32.  * Revision 3.19  1998/07/17 03:23:32  brianp
  33.  * added Pixel.ScaleOrBiasRGBA field
  34.  *
  35.  * Revision 3.18  1998/06/21 02:00:55  brianp
  36.  * cleaned up clip interpolation function code
  37.  *
  38.  * Revision 3.17  1998/06/19 03:13:10  brianp
  39.  * evaluator state wasn't fully initialized
  40.  *
  41.  * Revision 3.16  1998/06/07 22:18:52  brianp
  42.  * implemented GL_EXT_multitexture extension
  43.  *
  44.  * Revision 3.15  1998/04/01 02:58:28  brianp
  45.  * updated for v0.24 of 3Dfx/Glide driver
  46.  *
  47.  * Revision 3.14  1998/03/27 03:30:36  brianp
  48.  * fixed G++ warnings
  49.  *
  50.  * Revision 3.13  1998/03/22 16:42:22  brianp
  51.  * added 8-bit CI->RGBA pixel mapping tables
  52.  *
  53.  * Revision 3.12  1998/03/15 17:55:54  brianp
  54.  * added FogMode to context struct
  55.  *
  56.  * Revision 3.11  1998/03/05 02:49:48  brianp
  57.  * added an assertion and added better comments in gl_update_state()
  58.  *
  59.  * Revision 3.10  1998/03/01 20:17:03  brianp
  60.  * removed dead code
  61.  *
  62.  * Revision 3.9  1998/02/20 04:50:09  brianp
  63.  * implemented GL_SGIS_multitexture
  64.  *
  65.  * Revision 3.8  1998/02/13 04:38:05  brianp
  66.  * optimized blending functions called via BlendFunc function pointer
  67.  *
  68.  * Revision 3.7  1998/02/13 03:23:04  brianp
  69.  * AlphaRef is now a GLubyte
  70.  *
  71.  * Revision 3.6  1998/02/13 03:17:02  brianp
  72.  * added basic stereo support
  73.  *
  74.  * Revision 3.5  1998/02/08 20:20:34  brianp
  75.  * ColorMask is now GLubyte[4] instead of GLuint
  76.  *
  77.  * Revision 3.4  1998/02/05 01:10:25  brianp
  78.  * added John Stone's thread modifications
  79.  *
  80.  * Revision 3.3  1998/02/02 03:09:34  brianp
  81.  * added GL_LIGHT_MODEL_COLOR_CONTROL (separate specular color interpolation)
  82.  *
  83.  * Revision 3.2  1998/02/01 16:37:19  brianp
  84.  * added GL_EXT_rescale_normal extension
  85.  *
  86.  * Revision 3.1  1998/01/31 23:56:55  brianp
  87.  * removed Driver.ClearDepthBuffer function
  88.  *
  89.  * Revision 3.0  1998/01/31 20:49:07  brianp
  90.  * initial rev
  91.  *
  92.  */
  93.  
  94.  
  95. /*
  96.  * If multi-threading is enabled (-DTHREADS) then each thread has it's
  97.  * own rendering context.  A thread obtains the pointer to its GLcontext
  98.  * with the gl_get_thread_context() function.  Otherwise, the global
  99.  * pointer, CC, points to the current context used by all threads in
  100.  * the address space.
  101.  */
  102.  
  103.  
  104. #ifdef PC_HEADER
  105. #include "all.h"
  106. #else
  107. #include <assert.h>
  108. #include <math.h>
  109. #include <stdio.h>
  110. #include <stdlib.h>
  111. #include <string.h>
  112. #include "accum.h"
  113. #include "alphabuf.h"
  114. #include "clip.h"
  115. #include "context.h"
  116. #include "depth.h"
  117. #include "eval.h"
  118. #include "hash.h"
  119. #include "light.h"
  120. #include "lines.h"
  121. #include "dlist.h"
  122. #include "macros.h"
  123. #include "mmath.h"
  124. #include "pb.h"
  125. #include "points.h"
  126. #include "pointers.h"
  127. #include "quads.h"
  128. #include "stencil.h"
  129. #include "triangle.h"
  130. #include "teximage.h"
  131. #include "texobj.h"
  132. #include "texstate.h"
  133. #include "types.h"
  134. #include "vb.h"
  135. #include "vbfill.h"
  136. #endif
  137.  
  138. #ifdef __PPC__ /* additional profiling */
  139. extern  GLdouble prof1_time,prof2_time,prof3_time,prof4_time,prof5_time,prof6_time,prof7_time,prof8_time,profrender_time;
  140. #endif
  141.  
  142. /**********************************************************************/
  143. /*****                  Context and Thread management             *****/
  144. /**********************************************************************/
  145.  
  146.  
  147. #ifdef THREADS
  148.  
  149. #include "mthreads.h" /* Mesa platform independent threads interface */
  150.  
  151. static MesaTSD mesa_ctx_tsd;
  152.  
  153. static void mesa_ctx_thread_init() {
  154.   MesaInitTSD(&mesa_ctx_tsd);
  155. }
  156.  
  157. GLcontext *gl_get_thread_context( void ) {
  158.   return (GLcontext *) MesaGetTSD(&mesa_ctx_tsd);
  159. }
  160.  
  161. static void set_thread_context( GLcontext *ctx ) {
  162.   MesaSetTSD(&mesa_ctx_tsd, ctx, mesa_ctx_thread_init);
  163. }
  164.  
  165.  
  166. #else
  167.  
  168. /* One Current Context pointer for all threads in the address space */
  169. GLcontext *CC = NULL;
  170.  
  171. #endif /*THREADS*/
  172.  
  173.  
  174. /**********************************************************************/
  175. /*****                   Profiling functions                      *****/
  176. /**********************************************************************/
  177.  
  178. #ifdef PROFILE
  179.  
  180. #ifndef AMIGA
  181. #include <sys/times.h>
  182. #include <sys/param.h>
  183.  
  184.  
  185. /*
  186.  * Return system time in seconds.
  187.  * NOTE:  this implementation may not be very portable!
  188.  */
  189. GLdouble gl_time( void )
  190. {
  191.    static GLdouble prev_time = 0.0;
  192.    static GLdouble time;
  193.    struct tms tm;
  194.    clock_t clk;
  195.  
  196.    clk = times(&tm);
  197.  
  198. #ifdef CLK_TCK
  199.    time = (double)clk / (double)CLK_TCK;
  200. #else
  201.    time = (double)clk / (double)HZ;
  202. #endif
  203.  
  204.    if (time>prev_time) {
  205.       prev_time = time;
  206.       return time;
  207.    }
  208.    else {
  209.       return prev_time;
  210.    }
  211. }
  212.  
  213. #else
  214.  
  215. #ifdef __PPC__
  216.  
  217. #include <devices/timer.h>
  218. #include <clib/powerpc_protos.h>
  219.  
  220.  
  221. GLdouble gl_time( void )
  222. {
  223.    static GLdouble prev_time = 0.0;
  224.    static GLdouble time;
  225.    struct timeval tv;
  226.  
  227.    GetSysTimePPC(&tv);
  228.    time = ((GLdouble)tv.tv_secs + (GLdouble)tv.tv_micro / 1000000);
  229.    if (time>prev_time) {
  230.       prev_time = time;
  231.       return time;
  232.    }
  233.    else {
  234.       return prev_time;
  235.    }
  236. }
  237.  
  238. #endif
  239. #endif
  240.  
  241. /*
  242.  * Reset the timing/profiling counters
  243.  */
  244. static void init_timings( GLcontext *ctx )
  245. {
  246.    ctx->BeginEndCount = 0;
  247.    ctx->BeginEndTime = 0.0;
  248.    ctx->VertexCount = 0;
  249.    ctx->VertexTime = 0.0;
  250.    ctx->PointCount = 0;
  251.    ctx->PointTime = 0.0;
  252.    ctx->LineCount = 0;
  253.    ctx->LineTime = 0.0;
  254.    ctx->PolygonCount = 0;
  255.    ctx->PolygonTime = 0.0;
  256.    ctx->ClearCount = 0;
  257.    ctx->ClearTime = 0.0;
  258.    ctx->SwapCount = 0;
  259.    ctx->SwapTime = 0.0;
  260. }
  261.  
  262.  
  263.  
  264. /*
  265.  * Print the accumulated timing/profiling data.
  266.  */
  267. static void print_timings( GLcontext *ctx )
  268. {
  269.    GLdouble beginendrate;
  270.    GLdouble vertexrate;
  271.    GLdouble pointrate;
  272.    GLdouble linerate;
  273.    GLdouble polygonrate;
  274.    GLdouble overhead;
  275.    GLdouble clearrate;
  276.    GLdouble swaprate;
  277.    GLdouble avgvertices;
  278.  
  279.    if (ctx->BeginEndTime>0.0) {
  280.       beginendrate = ctx->BeginEndCount / ctx->BeginEndTime;
  281.    }
  282.    else {
  283.       beginendrate = 0.0;
  284.    }
  285.    if (ctx->VertexTime>0.0) {
  286.       vertexrate = ctx->VertexCount / ctx->VertexTime;
  287.    }
  288.    else {
  289.       vertexrate = 0.0;
  290.    }
  291.    if (ctx->PointTime>0.0) {
  292.       pointrate = ctx->PointCount / ctx->PointTime;
  293.    }
  294.    else {
  295.       pointrate = 0.0;
  296.    }
  297.    if (ctx->LineTime>0.0) {
  298.       linerate = ctx->LineCount / ctx->LineTime;
  299.    }
  300.    else {
  301.       linerate = 0.0;
  302.    }
  303.    if (ctx->PolygonTime>0.0) {
  304.       polygonrate = ctx->PolygonCount / ctx->PolygonTime;
  305.    }
  306.    else {
  307.       polygonrate = 0.0;
  308.    }
  309.    if (ctx->ClearTime>0.0) {
  310.       clearrate = ctx->ClearCount / ctx->ClearTime;
  311.    }
  312.    else {
  313.       clearrate = 0.0;
  314.    }
  315.    if (ctx->SwapTime>0.0) {
  316.       swaprate = ctx->SwapCount / ctx->SwapTime;
  317.    }
  318.    else {
  319.       swaprate = 0.0;
  320.    }
  321.  
  322.    if (ctx->BeginEndCount>0) {
  323.       avgvertices = (GLdouble) ctx->VertexCount / (GLdouble) ctx->BeginEndCount;
  324.    }
  325.    else {
  326.       avgvertices = 0.0;
  327.    }
  328.  
  329.    overhead = ctx->BeginEndTime - ctx->VertexTime - ctx->PointTime
  330.           - ctx->LineTime - ctx->PolygonTime;
  331.  
  332.  
  333.    printf("                          Count   Time (s)    Rate (/s) \n");
  334.    printf("--------------------------------------------------------\n");
  335.    printf("glBegin/glEnd           %7d  %8.3f   %10.3f\n",
  336.       ctx->BeginEndCount, ctx->BeginEndTime, beginendrate);
  337.    printf("  vertexes transformed  %7d  %8.3f   %10.3f\n",
  338.       ctx->VertexCount, ctx->VertexTime, vertexrate );
  339.    printf("    transformation           %8.3f\n",prof1_time);
  340.    printf("    clipping                 %8.3f\n",prof2_time);
  341.    printf("    projection               %8.3f\n",prof3_time);
  342.    printf("    lighting                 %8.3f\n",prof4_time);
  343.    printf("    per-vertex fogging       %8.3f\n",prof5_time);
  344.    printf("    texcoord transformation  %8.3f\n",prof6_time);
  345.    printf("    viewport calculations    %8.3f\n",prof7_time);
  346.    printf("    raster setup             %8.3f\n",prof8_time);
  347.    printf("  render vertex buffer       %8.3f\n",profrender_time);
  348.  
  349.    printf("  points rasterized     %7d  %8.3f   %10.3f\n",
  350.       ctx->PointCount, ctx->PointTime, pointrate );
  351.    printf("  lines rasterized      %7d  %8.3f   %10.3f\n",
  352.       ctx->LineCount, ctx->LineTime, linerate );
  353.    printf("  polygons rasterized   %7d  %8.3f   %10.3f\n",
  354.       ctx->PolygonCount, ctx->PolygonTime, polygonrate );
  355.    printf("  overhead                       %8.3f\n", overhead );
  356.    printf("glClear                 %7d  %8.3f   %10.3f\n",
  357.       ctx->ClearCount, ctx->ClearTime, clearrate );
  358.    printf("SwapBuffers             %7d  %8.3f   %10.3f\n",
  359.       ctx->SwapCount, ctx->SwapTime, swaprate );
  360.    printf("\n");
  361.  
  362.    printf("Average number of vertices per begin/end: %8.3f\n", avgvertices );
  363. }
  364. #endif
  365.  
  366.  
  367.  
  368.  
  369.  
  370. /**********************************************************************/
  371. /*****       Context allocation, initialization, destroying       *****/
  372. /**********************************************************************/
  373.  
  374.  
  375. /*
  376.  * Allocate and initialize a shared context state structure.
  377.  */
  378. static struct gl_shared_state *alloc_shared_state( void )
  379. {
  380.    GLuint i;
  381.    struct gl_shared_state *ss;
  382.    GLboolean outOfMemory;
  383.  
  384.    ss = (struct gl_shared_state*) calloc( 1, sizeof(struct gl_shared_state) );
  385.    if (!ss)
  386.       return NULL;
  387.  
  388.    ss->DisplayList = NewHashTable();
  389.  
  390.    ss->TexObjects = NewHashTable();
  391.  
  392.    /* Default Texture objects */
  393.    outOfMemory = GL_FALSE;
  394.    for (i=0;i<MAX_TEX_SETS;i++) {
  395.       ss->Default1D[i] = gl_alloc_texture_object(ss, 0, 1);
  396.       ss->Default2D[i] = gl_alloc_texture_object(ss, 0, 2);
  397.       ss->Default3D[i] = gl_alloc_texture_object(ss, 0, 3);
  398.       if (!ss->Default1D[i] || !ss->Default2D[i] || !ss->Default3D[i]) {
  399.      outOfMemory = GL_TRUE;
  400.      break;
  401.       }
  402.    }
  403.  
  404.    if (!ss->DisplayList || !ss->TexObjects || outOfMemory) {
  405.       /* Ran out of memory at some point.  Free everything and return NULL */
  406.       if (!ss->DisplayList)
  407.      DeleteHashTable(ss->DisplayList);
  408.       if (!ss->TexObjects)
  409.      DeleteHashTable(ss->TexObjects);
  410.       for (i=0;i<MAX_TEX_SETS;i++) {
  411.      if (!ss->Default1D[i])
  412.         gl_free_texture_object(ss, ss->Default1D[i]);
  413.      if (!ss->Default2D[i])
  414.         gl_free_texture_object(ss, ss->Default2D[i]);
  415.      if (!ss->Default3D[i])
  416.         gl_free_texture_object(ss, ss->Default3D[i]);
  417.       }
  418.       free(ss);
  419.       return NULL;
  420.    }
  421.    else {
  422.       return ss;
  423.    }
  424. }
  425.  
  426.  
  427. /*
  428.  * Deallocate a shared state context and all children structures.
  429.  */
  430. static void free_shared_state( GLcontext *ctx, struct gl_shared_state *ss )
  431. {
  432.    /* Free display lists */
  433.    while (1) {
  434.       GLuint list = HashFirstEntry(ss->DisplayList);
  435.       if (list) {
  436.      gl_destroy_list(ctx, list);
  437.       }
  438.       else {
  439.      break;
  440.       }
  441.    }
  442.    DeleteHashTable(ss->DisplayList);
  443.  
  444.    /* Free texture objects */
  445.    while (ss->TexObjectList)
  446.    {
  447.  
  448. #if 0 /* dangerous! */
  449.  
  450.       /* tell device driver to delete texture */
  451.     if (ctx->Driver.DeleteTexture) {
  452.         (*ctx->Driver.DeleteTexture)( ctx, ss->TexObjectList );
  453.     }
  454. #endif
  455.  
  456.       /* this function removes from linked list too! */
  457.       gl_free_texture_object(ss, ss->TexObjectList);
  458.    }
  459.    DeleteHashTable(ss->TexObjects);
  460.  
  461.    free(ss);
  462. }
  463.  
  464.  
  465.  
  466.  
  467. /*
  468.  * Initialize the nth light.  Note that the defaults for light 0 are
  469.  * different than the other lights.
  470.  */
  471. static void init_light( struct gl_light *l, GLuint n )
  472. {
  473.    ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
  474.    if (n==0) {
  475.       ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
  476.       ASSIGN_4V( l->Specular, 1.0, 1.0, 1.0, 1.0 );
  477.    }
  478.    else {
  479.       ASSIGN_4V( l->Diffuse, 0.0, 0.0, 0.0, 1.0 );
  480.       ASSIGN_4V( l->Specular, 0.0, 0.0, 0.0, 1.0 );
  481.    }
  482.    ASSIGN_4V( l->Position, 0.0, 0.0, 1.0, 0.0 );
  483.    ASSIGN_3V( l->Direction, 0.0, 0.0, -1.0 );
  484.    l->SpotExponent = 0.0;
  485.    gl_compute_spot_exp_table( l );
  486.    l->SpotCutoff = 180.0;
  487.    l->CosCutoff = -1.0;
  488.    l->ConstantAttenuation = 1.0;
  489.    l->LinearAttenuation = 0.0;
  490.    l->QuadraticAttenuation = 0.0;
  491.    l->Enabled = GL_FALSE;
  492. }
  493.  
  494.  
  495.  
  496. static void init_lightmodel( struct gl_lightmodel *lm )
  497. {
  498.    ASSIGN_4V( lm->Ambient, 0.2, 0.2, 0.2, 1.0 );
  499.    lm->LocalViewer = GL_FALSE;
  500.    lm->TwoSide = GL_FALSE;
  501.    lm->ColorControl = GL_SINGLE_COLOR;
  502. }
  503.  
  504.  
  505. static void init_material( struct gl_material *m )
  506. {
  507.    ASSIGN_4V( m->Ambient,  0.2, 0.2, 0.2, 1.0 );
  508.    ASSIGN_4V( m->Diffuse,  0.8, 0.8, 0.8, 1.0 );
  509.    ASSIGN_4V( m->Specular, 0.0, 0.0, 0.0, 1.0 );
  510.    ASSIGN_4V( m->Emission, 0.0, 0.0, 0.0, 1.0 );
  511.    m->Shininess = 0.0;
  512.    m->AmbientIndex = 0;
  513.    m->DiffuseIndex = 1;
  514.    m->SpecularIndex = 1;
  515.    gl_compute_material_shine_table( m );
  516. }
  517.  
  518.  
  519. static void init_texture_set( GLcontext *ctx, GLuint setNum )
  520. {
  521.    struct gl_texture_set *s = &ctx->Texture.Set[setNum];
  522.  
  523.    s->EnvMode = GL_MODULATE;
  524.    ASSIGN_4V( s->EnvColor, 0.0, 0.0, 0.0, 0.0 );
  525.    s->TexGenEnabled = 0;
  526.    s->GenModeS = GL_EYE_LINEAR;
  527.    s->GenModeT = GL_EYE_LINEAR;
  528.    s->GenModeR = GL_EYE_LINEAR;
  529.    s->GenModeQ = GL_EYE_LINEAR;
  530.    /* Yes, these plane coefficients are correct! */
  531.    ASSIGN_4V( s->ObjectPlaneS, 1.0, 0.0, 0.0, 0.0 );
  532.    ASSIGN_4V( s->ObjectPlaneT, 0.0, 1.0, 0.0, 0.0 );
  533.    ASSIGN_4V( s->ObjectPlaneR, 0.0, 0.0, 0.0, 0.0 );
  534.    ASSIGN_4V( s->ObjectPlaneQ, 0.0, 0.0, 0.0, 0.0 );
  535.    ASSIGN_4V( s->EyePlaneS, 1.0, 0.0, 0.0, 0.0 );
  536.    ASSIGN_4V( s->EyePlaneT, 0.0, 1.0, 0.0, 0.0 );
  537.    ASSIGN_4V( s->EyePlaneR, 0.0, 0.0, 0.0, 0.0 );
  538.    ASSIGN_4V( s->EyePlaneQ, 0.0, 0.0, 0.0, 0.0 );
  539.  
  540.    s->Current1D = ctx->Shared->Default1D[setNum];
  541.    s->Current2D = ctx->Shared->Default2D[setNum];
  542.    s->Current3D = ctx->Shared->Default3D[setNum];
  543.  
  544.    s->TexCoordSet = setNum;   /* GL_EXT_multitexture */
  545. }
  546.  
  547.  
  548. /* Initialize a 1-D evaluator map */
  549. static void init_1d_map( struct gl_1d_map *map, int n, const float *initial )
  550. {
  551.    map->Order = 1;
  552.    map->u1 = 0.0;
  553.    map->u2 = 1.0;
  554.    map->Points = malloc(n * sizeof(GLfloat));
  555.    if (map->Points) {
  556.       GLint i;
  557.       for (i=0;i<n;i++)
  558.      map->Points[i] = initial[i];
  559.    }
  560.    map->Retain = GL_FALSE;
  561. }
  562.  
  563.  
  564. /* Initialize a 2-D evaluator map */
  565. static void init_2d_map( struct gl_2d_map *map, int n, const float *initial )
  566. {
  567.    map->Uorder = 1;
  568.    map->Vorder = 1;
  569.    map->u1 = 0.0;
  570.    map->u2 = 1.0;
  571.    map->v1 = 0.0;
  572.    map->v2 = 1.0;
  573.    map->Points = malloc(n * sizeof(GLfloat));
  574.    if (map->Points) {
  575.       GLint i;
  576.       for (i=0;i<n;i++)
  577.      map->Points[i] = initial[i];
  578.    }
  579.    map->Retain = GL_FALSE;
  580. }
  581.  
  582.  
  583.  
  584. /*
  585.  * Initialize a gl_context structure to default values.
  586.  */
  587. static void initialize_context( GLcontext *ctx )
  588. {
  589.    static GLfloat identity[16] = {
  590.     1.0, 0.0, 0.0, 0.0,
  591.     0.0, 1.0, 0.0, 0.0,
  592.     0.0, 0.0, 1.0, 0.0,
  593.     0.0, 0.0, 0.0, 1.0
  594.    };
  595.    GLuint i;
  596.  
  597.    if (ctx) {
  598.       /* Modelview matrix */
  599.       ctx->NewModelViewMatrix = GL_FALSE;
  600.       ctx->ModelViewMatrixType = MATRIX_IDENTITY;
  601.       MEMCPY( ctx->ModelViewMatrix, identity, 16*sizeof(GLfloat) );
  602.       MEMCPY( ctx->ModelViewInv, identity, 16*sizeof(GLfloat) );
  603.       ctx->ModelViewStackDepth = 0;
  604.  
  605.       /* Projection matrix */
  606.       ctx->NewProjectionMatrix = GL_FALSE;
  607.       ctx->ProjectionMatrixType = MATRIX_IDENTITY;
  608.       MEMCPY( ctx->ProjectionMatrix, identity, 16*sizeof(GLfloat) );
  609.       ctx->ProjectionStackDepth = 0;
  610.       ctx->NearFarStack[0][0] = 1.0; /* These values seem weird by make */
  611.       ctx->NearFarStack[0][1] = 0.0; /* sense mathematically. */
  612.  
  613.       /* Texture matrix */
  614.       for (i=0; i<MAX_TEX_SETS; i++) {
  615.      ctx->NewTextureMatrix = GL_FALSE;
  616.      ctx->TextureMatrixType[i] = MATRIX_IDENTITY;
  617.      MEMCPY( ctx->TextureMatrix[i], identity, 16*sizeof(GLfloat) );
  618.      ctx->TextureStackDepth[i] = 0;
  619.       }
  620.  
  621.       /* Accumulate buffer group */
  622.       ASSIGN_4V( ctx->Accum.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  623.  
  624.       /* Color buffer group */
  625.       ctx->Color.IndexMask = 0xffffffff;
  626.       ctx->Color.ColorMask[0] = 0xff;
  627.       ctx->Color.ColorMask[1] = 0xff;
  628.       ctx->Color.ColorMask[2] = 0xff;
  629.       ctx->Color.ColorMask[3] = 0xff;
  630.       ctx->Color.SWmasking = GL_FALSE;
  631.       ctx->Color.ClearIndex = 0;
  632.       ASSIGN_4V( ctx->Color.ClearColor, 0.0, 0.0, 0.0, 0.0 );
  633.       ctx->Color.DrawBuffer = GL_FRONT;
  634.       ctx->Color.AlphaEnabled = GL_FALSE;
  635.       ctx->Color.AlphaFunc = GL_ALWAYS;
  636.       ctx->Color.AlphaRef = 0;
  637.       ctx->Color.BlendEnabled = GL_FALSE;
  638.       ctx->Color.BlendSrc = GL_ONE;
  639.       ctx->Color.BlendDst = GL_ZERO;
  640.       ctx->Color.BlendEquation = GL_FUNC_ADD_EXT;
  641.       ctx->Color.BlendFunc = NULL;  /* this pointer set only when needed */
  642.       ASSIGN_4V( ctx->Color.BlendColor, 0.0, 0.0, 0.0, 0.0 );
  643.       ctx->Color.IndexLogicOpEnabled = GL_FALSE;
  644.       ctx->Color.ColorLogicOpEnabled = GL_FALSE;
  645.       ctx->Color.SWLogicOpEnabled = GL_FALSE;
  646.       ctx->Color.LogicOp = GL_COPY;
  647.       ctx->Color.DitherFlag = GL_TRUE;
  648.  
  649.       /* Current group */
  650.       ASSIGN_4V( ctx->Current.ByteColor, 255, 255, 255, 255 );
  651.       ctx->Current.Index = 1;
  652.       ASSIGN_3V( ctx->Current.Normal, 0.0, 0.0, 1.0 );
  653.       for (i=0; i<MAX_TEX_SETS; i++)
  654.      ASSIGN_4V( ctx->Current.MultiTexCoord[i], 0.0, 0.0, 0.0, 1.0 );
  655.       ctx->Current.TexCoord = ctx->Current.MultiTexCoord[0];
  656.       ASSIGN_4V( ctx->Current.RasterPos, 0.0, 0.0, 0.0, 1.0 );
  657.       ctx->Current.RasterDistance = 0.0;
  658.       ASSIGN_4V( ctx->Current.RasterColor, 1.0, 1.0, 1.0, 1.0 );
  659.       ctx->Current.RasterIndex = 1;
  660.       for (i=0; i<MAX_TEX_SETS; i++)
  661.      ASSIGN_4V( ctx->Current.RasterMultiTexCoord[i], 0.0, 0.0, 0.0, 1.0 );
  662.       ctx->Current.RasterTexCoord = ctx->Current.RasterMultiTexCoord[0];
  663.       ctx->Current.RasterPosValid = GL_TRUE;
  664.       ctx->Current.EdgeFlag = GL_TRUE;
  665.  
  666.       /* Depth buffer group */
  667.       ctx->Depth.Test = GL_FALSE;
  668.       ctx->Depth.Clear = 1.0;
  669.       ctx->Depth.Func = GL_LESS;
  670.       ctx->Depth.Mask = GL_TRUE;
  671.  
  672.       /* Evaluators group */
  673.       ctx->Eval.Map1Color4 = GL_FALSE;
  674.       ctx->Eval.Map1Index = GL_FALSE;
  675.       ctx->Eval.Map1Normal = GL_FALSE;
  676.       ctx->Eval.Map1TextureCoord1 = GL_FALSE;
  677.       ctx->Eval.Map1TextureCoord2 = GL_FALSE;
  678.       ctx->Eval.Map1TextureCoord3 = GL_FALSE;
  679.       ctx->Eval.Map1TextureCoord4 = GL_FALSE;
  680.       ctx->Eval.Map1Vertex3 = GL_FALSE;
  681.       ctx->Eval.Map1Vertex4 = GL_FALSE;
  682.       ctx->Eval.Map2Color4 = GL_FALSE;
  683.       ctx->Eval.Map2Index = GL_FALSE;
  684.       ctx->Eval.Map2Normal = GL_FALSE;
  685.       ctx->Eval.Map2TextureCoord1 = GL_FALSE;
  686.       ctx->Eval.Map2TextureCoord2 = GL_FALSE;
  687.       ctx->Eval.Map2TextureCoord3 = GL_FALSE;
  688.       ctx->Eval.Map2TextureCoord4 = GL_FALSE;
  689.       ctx->Eval.Map2Vertex3 = GL_FALSE;
  690.       ctx->Eval.Map2Vertex4 = GL_FALSE;
  691.       ctx->Eval.AutoNormal = GL_FALSE;
  692.       ctx->Eval.MapGrid1un = 1;
  693.       ctx->Eval.MapGrid1u1 = 0.0;
  694.       ctx->Eval.MapGrid1u2 = 1.0;
  695.       ctx->Eval.MapGrid2un = 1;
  696.       ctx->Eval.MapGrid2vn = 1;
  697.       ctx->Eval.MapGrid2u1 = 0.0;
  698.       ctx->Eval.MapGrid2u2 = 1.0;
  699.       ctx->Eval.MapGrid2v1 = 0.0;
  700.       ctx->Eval.MapGrid2v2 = 1.0;
  701.  
  702.       /* Evaluator data */
  703.       {
  704.      static GLfloat vertex[4] = { 0.0, 0.0, 0.0, 1.0 };
  705.      static GLfloat normal[3] = { 0.0, 0.0, 1.0 };
  706.      static GLfloat index[1] = { 1.0 };
  707.      static GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
  708.      static GLfloat texcoord[4] = { 0.0, 0.0, 0.0, 1.0 };
  709.  
  710.      init_1d_map( &ctx->EvalMap.Map1Vertex3, 3, vertex );
  711.      init_1d_map( &ctx->EvalMap.Map1Vertex4, 4, vertex );
  712.      init_1d_map( &ctx->EvalMap.Map1Index, 1, index );
  713.      init_1d_map( &ctx->EvalMap.Map1Color4, 4, color );
  714.      init_1d_map( &ctx->EvalMap.Map1Normal, 3, normal );
  715.      init_1d_map( &ctx->EvalMap.Map1Texture1, 1, texcoord );
  716.      init_1d_map( &ctx->EvalMap.Map1Texture2, 2, texcoord );
  717.      init_1d_map( &ctx->EvalMap.Map1Texture3, 3, texcoord );
  718.      init_1d_map( &ctx->EvalMap.Map1Texture4, 4, texcoord );
  719.  
  720.      init_2d_map( &ctx->EvalMap.Map2Vertex3, 3, vertex );
  721.      init_2d_map( &ctx->EvalMap.Map2Vertex4, 4, vertex );
  722.      init_2d_map( &ctx->EvalMap.Map2Index, 1, index );
  723.      init_2d_map( &ctx->EvalMap.Map2Color4, 4, color );
  724.      init_2d_map( &ctx->EvalMap.Map2Normal, 3, normal );
  725.      init_2d_map( &ctx->EvalMap.Map2Texture1, 1, texcoord );
  726.      init_2d_map( &ctx->EvalMap.Map2Texture2, 2, texcoord );
  727.      init_2d_map( &ctx->EvalMap.Map2Texture3, 3, texcoord );
  728.      init_2d_map( &ctx->EvalMap.Map2Texture4, 4, texcoord );
  729.       }
  730.  
  731.       /* Fog group */
  732.       ctx->Fog.Enabled = GL_FALSE;
  733.       ctx->Fog.Mode = GL_EXP;
  734.       ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
  735.       ctx->Fog.Index = 0.0;
  736.       ctx->Fog.Density = 1.0;
  737.       ctx->Fog.Start = 0.0;
  738.       ctx->Fog.End = 1.0;
  739.  
  740.       /* Hint group */
  741.       ctx->Hint.PerspectiveCorrection = GL_DONT_CARE;
  742.       ctx->Hint.PointSmooth = GL_DONT_CARE;
  743.       ctx->Hint.LineSmooth = GL_DONT_CARE;
  744.       ctx->Hint.PolygonSmooth = GL_DONT_CARE;
  745.       ctx->Hint.Fog = GL_DONT_CARE;
  746.  
  747.       /* Lighting group */
  748.       for (i=0;i<MAX_LIGHTS;i++) {
  749.      init_light( &ctx->Light.Light[i], i );
  750.       }
  751.       init_lightmodel( &ctx->Light.Model );
  752.       init_material( &ctx->Light.Material[0] );
  753.       init_material( &ctx->Light.Material[1] );
  754.       ctx->Light.ShadeModel = GL_SMOOTH;
  755.       ctx->Light.Enabled = GL_FALSE;
  756.       ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
  757.       ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
  758.       ctx->Light.ColorMaterialBitmask
  759.      = gl_material_bitmask( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
  760.  
  761.       ctx->Light.ColorMaterialEnabled = GL_FALSE;
  762.  
  763.       /* Line group */
  764.       ctx->Line.SmoothFlag = GL_FALSE;
  765.       ctx->Line.StippleFlag = GL_FALSE;
  766.       ctx->Line.Width = 1.0;
  767.       ctx->Line.StipplePattern = 0xffff;
  768.       ctx->Line.StippleFactor = 1;
  769.  
  770.       /* Display List group */
  771.       ctx->List.ListBase = 0;
  772.  
  773.       /* Pixel group */
  774.       ctx->Pixel.RedBias = 0.0;
  775.       ctx->Pixel.RedScale = 1.0;
  776.       ctx->Pixel.GreenBias = 0.0;
  777.       ctx->Pixel.GreenScale = 1.0;
  778.       ctx->Pixel.BlueBias = 0.0;
  779.       ctx->Pixel.BlueScale = 1.0;
  780.       ctx->Pixel.AlphaBias = 0.0;
  781.       ctx->Pixel.AlphaScale = 1.0;
  782.       ctx->Pixel.ScaleOrBiasRGBA = GL_FALSE;
  783.       ctx->Pixel.DepthBias = 0.0;
  784.       ctx->Pixel.DepthScale = 1.0;
  785.       ctx->Pixel.IndexOffset = 0;
  786.       ctx->Pixel.IndexShift = 0;
  787.       ctx->Pixel.ZoomX = 1.0;
  788.       ctx->Pixel.ZoomY = 1.0;
  789.       ctx->Pixel.MapColorFlag = GL_FALSE;
  790.       ctx->Pixel.MapStencilFlag = GL_FALSE;
  791.       ctx->Pixel.MapStoSsize = 1;
  792.       ctx->Pixel.MapItoIsize = 1;
  793.       ctx->Pixel.MapItoRsize = 1;
  794.       ctx->Pixel.MapItoGsize = 1;
  795.       ctx->Pixel.MapItoBsize = 1;
  796.       ctx->Pixel.MapItoAsize = 1;
  797.       ctx->Pixel.MapRtoRsize = 1;
  798.       ctx->Pixel.MapGtoGsize = 1;
  799.       ctx->Pixel.MapBtoBsize = 1;
  800.       ctx->Pixel.MapAtoAsize = 1;
  801.       ctx->Pixel.MapStoS[0] = 0;
  802.       ctx->Pixel.MapItoI[0] = 0;
  803.       ctx->Pixel.MapItoR[0] = 0.0;
  804.       ctx->Pixel.MapItoG[0] = 0.0;
  805.       ctx->Pixel.MapItoB[0] = 0.0;
  806.       ctx->Pixel.MapItoA[0] = 0.0;
  807.       ctx->Pixel.MapItoR8[0] = 0;
  808.       ctx->Pixel.MapItoG8[0] = 0;
  809.       ctx->Pixel.MapItoB8[0] = 0;
  810.       ctx->Pixel.MapItoA8[0] = 0;
  811.       ctx->Pixel.MapRtoR[0] = 0.0;
  812.       ctx->Pixel.MapGtoG[0] = 0.0;
  813.       ctx->Pixel.MapBtoB[0] = 0.0;
  814.       ctx->Pixel.MapAtoA[0] = 0.0;
  815.  
  816.       /* Point group */
  817.       ctx->Point.SmoothFlag = GL_FALSE;
  818.       ctx->Point.Size = 1.0;
  819.       ctx->Point.Params[0] = 1.0;
  820.       ctx->Point.Params[1] = 0.0;
  821.       ctx->Point.Params[2] = 0.0;
  822.       ctx->Point.MinSize = 0.0;
  823.       ctx->Point.MaxSize = (GLfloat) MAX_POINT_SIZE;
  824.       ctx->Point.Threshold = 1.0;
  825.  
  826.       /* Polygon group */
  827.       ctx->Polygon.CullFlag = GL_FALSE;
  828.       ctx->Polygon.CullFaceMode = GL_BACK;
  829.       ctx->Polygon.FrontFace = GL_CCW;
  830.       ctx->Polygon.FrontMode = GL_FILL;
  831.       ctx->Polygon.BackMode = GL_FILL;
  832.       ctx->Polygon.Unfilled = GL_FALSE;
  833.       ctx->Polygon.SmoothFlag = GL_FALSE;
  834.       ctx->Polygon.StippleFlag = GL_FALSE;
  835.       ctx->Polygon.OffsetFactor = 0.0F;
  836.       ctx->Polygon.OffsetUnits = 0.0F;
  837.       ctx->Polygon.OffsetPoint = GL_FALSE;
  838.       ctx->Polygon.OffsetLine = GL_FALSE;
  839.       ctx->Polygon.OffsetFill = GL_FALSE;
  840.       ctx->Polygon.OffsetAny = GL_FALSE;
  841.  
  842.       /* Polygon Stipple group */
  843.       MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
  844.  
  845.       /* Scissor group */
  846.       ctx->Scissor.Enabled = GL_FALSE;
  847.       ctx->Scissor.X = 0;
  848.       ctx->Scissor.Y = 0;
  849.       ctx->Scissor.Width = 0;
  850.       ctx->Scissor.Height = 0;
  851.  
  852.       /* Stencil group */
  853.       ctx->Stencil.Enabled = GL_FALSE;
  854.       ctx->Stencil.Function = GL_ALWAYS;
  855.       ctx->Stencil.FailFunc = GL_KEEP;
  856.       ctx->Stencil.ZPassFunc = GL_KEEP;
  857.       ctx->Stencil.ZFailFunc = GL_KEEP;
  858.       ctx->Stencil.Ref = 0;
  859.       ctx->Stencil.ValueMask = 0xff;
  860.       ctx->Stencil.Clear = 0;
  861.       ctx->Stencil.WriteMask = 0xff;
  862.  
  863.       /* Texture group */
  864.       ctx->Texture.CurrentSet = 0;      /* GL_SGIS_multitexture */
  865.       ctx->Texture.CurrentTransformSet = 0; /* GL_EXT_multitexture */
  866.       ctx->Texture.Enabled = 0;
  867.  
  868.       ctx->Texture.AnyDirty = GL_FALSE;
  869.       for (i=0; i<MAX_TEX_SETS; i++)
  870.      init_texture_set( ctx, i );
  871.  
  872.       ctx->Texture.SharedPalette = GL_FALSE;
  873.       ctx->Texture.Palette[0] = 255;
  874.       ctx->Texture.Palette[1] = 255;
  875.       ctx->Texture.Palette[2] = 255;
  876.       ctx->Texture.Palette[3] = 255;
  877.       ctx->Texture.PaletteSize = 1;
  878.       ctx->Texture.PaletteIntFormat = GL_RGBA;
  879.       ctx->Texture.PaletteFormat = GL_RGBA;
  880.  
  881.       /* Transformation group */
  882.       ctx->Transform.MatrixMode = GL_MODELVIEW;
  883.       ctx->Transform.Normalize = GL_FALSE;
  884.       ctx->Transform.RescaleNormals = GL_FALSE;
  885.       for (i=0;i<MAX_CLIP_PLANES;i++) {
  886.      ctx->Transform.ClipEnabled[i] = GL_FALSE;
  887.      ASSIGN_4V( ctx->Transform.ClipEquation[i], 0.0, 0.0, 0.0, 0.0 );
  888.       }
  889.       ctx->Transform.AnyClip = GL_FALSE;
  890.  
  891.       /* Viewport group */
  892.       ctx->Viewport.X = 0;
  893.       ctx->Viewport.Y = 0;
  894.       ctx->Viewport.Width = 0;
  895.       ctx->Viewport.Height = 0;   
  896.       ctx->Viewport.Near = 0.0;
  897.       ctx->Viewport.Far = 1.0;
  898.       ctx->Viewport.Sx = 0.0;  /* Sx, Tx, Sy, Ty are computed later */
  899.       ctx->Viewport.Tx = 0.0;
  900.       ctx->Viewport.Sy = 0.0;
  901.       ctx->Viewport.Ty = 0.0;
  902.       ctx->Viewport.Sz = 0.5 * DEPTH_SCALE;
  903.       ctx->Viewport.Tz = 0.5 * DEPTH_SCALE;
  904.  
  905.       /* Vertex arrays */
  906.       ctx->Array.VertexSize = 4;
  907.       ctx->Array.VertexType = GL_FLOAT;
  908.       ctx->Array.VertexStride = 0;
  909.       ctx->Array.VertexStrideB = 0;
  910.       ctx->Array.VertexPtr = NULL;
  911.       ctx->Array.VertexEnabled = GL_FALSE;
  912.       ctx->Array.NormalType = GL_FLOAT;
  913.       ctx->Array.NormalStride = 0;
  914.       ctx->Array.NormalStrideB = 0;
  915.       ctx->Array.NormalPtr = NULL;
  916.       ctx->Array.NormalEnabled = GL_FALSE;
  917.       ctx->Array.ColorSize = 4;
  918.       ctx->Array.ColorType = GL_FLOAT;
  919.       ctx->Array.ColorStride = 0;
  920.       ctx->Array.ColorStrideB = 0;
  921.       ctx->Array.ColorPtr = NULL;
  922.       ctx->Array.ColorEnabled = GL_FALSE;
  923.       ctx->Array.IndexType = GL_FLOAT;
  924.       ctx->Array.IndexStride = 0;
  925.       ctx->Array.IndexStrideB = 0;
  926.       ctx->Array.IndexPtr = NULL;
  927.       ctx->Array.IndexEnabled = GL_FALSE;
  928.       for (i = 0; i < MAX_TEX_COORD_SETS; i++) {
  929.      ctx->Array.TexCoordSize[i] = 4;
  930.      ctx->Array.TexCoordType[i] = GL_FLOAT;
  931.      ctx->Array.TexCoordStride[i] = 0;
  932.      ctx->Array.TexCoordStrideB[i] = 0;
  933.      ctx->Array.TexCoordPtr[i] = NULL;
  934.      ctx->Array.TexCoordEnabled[i] = GL_FALSE;
  935.       }
  936.       ctx->Array.TexCoordInterleaveFactor = 1;
  937.       ctx->Array.EdgeFlagStride = 0;
  938.       ctx->Array.EdgeFlagStrideB = 0;
  939.       ctx->Array.EdgeFlagPtr = NULL;
  940.       ctx->Array.EdgeFlagEnabled = GL_FALSE;
  941.  
  942.       /* Pixel transfer */
  943.       ctx->Pack.Alignment = 4;
  944.       ctx->Pack.RowLength = 0;
  945.       ctx->Pack.SkipPixels = 0;
  946.       ctx->Pack.SkipRows = 0;
  947.       ctx->Pack.SwapBytes = GL_FALSE;
  948.       ctx->Pack.LsbFirst = GL_FALSE;
  949.       ctx->Unpack.Alignment = 4;
  950.       ctx->Unpack.RowLength = 0;
  951.       ctx->Unpack.SkipPixels = 0;
  952.       ctx->Unpack.SkipRows = 0;
  953.       ctx->Unpack.SwapBytes = GL_FALSE;
  954.       ctx->Unpack.LsbFirst = GL_FALSE;
  955.  
  956.       /* Feedback */
  957.       ctx->Feedback.Type = GL_2D;   /* TODO: verify */
  958.       ctx->Feedback.Buffer = NULL;
  959.       ctx->Feedback.BufferSize = 0;
  960.       ctx->Feedback.Count = 0;
  961.  
  962.       /* Selection/picking */
  963.       ctx->Select.Buffer = NULL;
  964.       ctx->Select.BufferSize = 0;
  965.       ctx->Select.BufferCount = 0;
  966.       ctx->Select.Hits = 0;
  967.       ctx->Select.NameStackDepth = 0;
  968.  
  969.       /* GL_EXT_multitexture */
  970.       ctx->TexCoordSet = 0;
  971.  
  972.       /* Renderer and client attribute stacks */
  973.       ctx->AttribStackDepth = 0;
  974.       ctx->ClientAttribStackDepth = 0;
  975.  
  976.       /*** Miscellaneous ***/
  977.       ctx->NewState = NEW_ALL;
  978.       ctx->RenderMode = GL_RENDER;
  979.       ctx->Primitive = GL_BITMAP;
  980.       ctx->StippleCounter = 0;
  981.       ctx->NeedNormals = GL_FALSE;
  982.       ctx->DoViewportMapping = GL_TRUE;
  983.  
  984.       /* Display list */
  985.       ctx->CallDepth = 0;
  986.       ctx->ExecuteFlag = GL_TRUE;
  987.       ctx->CompileFlag = GL_FALSE;
  988.       ctx->CurrentListPtr = NULL;
  989.       ctx->CurrentBlock = NULL;
  990.       ctx->CurrentListNum = 0;
  991.       ctx->CurrentPos = 0;
  992.  
  993.       ctx->ErrorValue = (GLenum) GL_NO_ERROR;
  994.  
  995.       /* For debug/development only */
  996.       ctx->NoRaster = getenv("MESA_NO_RASTER") ? GL_TRUE : GL_FALSE;
  997.  
  998.       /* Dither disable */
  999.       ctx->NoDither = getenv("MESA_NO_DITHER") ? GL_TRUE : GL_FALSE;
  1000.       if (ctx->NoDither) {
  1001.      if (getenv("MESA_DEBUG")) {
  1002.         fprintf(stderr, "MESA_NO_DITHER set - dithering disabled\n");
  1003.      }
  1004.      ctx->Color.DitherFlag = GL_FALSE;
  1005.       }
  1006.    }
  1007. }
  1008.  
  1009.  
  1010.  
  1011. /*
  1012.  * Allocate a new GLvisual object.
  1013.  * Input:  rgbFlag - GL_TRUE=RGB(A) mode, GL_FALSE=Color Index mode
  1014.  *         alphaFlag - alloc software alpha buffers?
  1015.  *         dbFlag - double buffering?
  1016.  *         stereoFlag - stereo buffer?
  1017.  *         depthFits - requested minimum bits per depth buffer value
  1018.  *         stencilFits - requested minimum bits per stencil buffer value
  1019.  *         accumFits - requested minimum bits per accum buffer component
  1020.  *         indexFits - number of bits per pixel if rgbFlag==GL_FALSE
  1021.  *         red/green/blue/alphaFits - number of bits per color component
  1022.  *                                     in frame buffer for RGB(A) mode.
  1023.  * Return:  pointer to new GLvisual or NULL if requested parameters can't
  1024.  *          be met.
  1025.  */
  1026. GLvisual *gl_create_visual( GLboolean rgbFlag,
  1027.                 GLboolean alphaFlag,
  1028.                 GLboolean dbFlag,
  1029.                 GLboolean stereoFlag,
  1030.                 GLint depthBits,
  1031.                 GLint stencilBits,
  1032.                 GLint accumBits,
  1033.                 GLint indexBits,
  1034.                 GLint redBits,
  1035.                 GLint greenBits,
  1036.                 GLint blueBits,
  1037.                 GLint alphaBits )
  1038. {
  1039.    GLvisual *vis;
  1040.  
  1041.    if (stereoFlag) {
  1042.       gl_warning(NULL, "stereo not supported");
  1043.       return NULL;   /* Stereo isn't supported yet! */
  1044.    }
  1045.  
  1046.    if (depthBits > (GLint) (8*sizeof(GLdepth))) {
  1047.       /* can't meet depth buffer requirements */
  1048.       return NULL;
  1049.    }
  1050.    if (stencilBits > (GLint) (8*sizeof(GLstencil))) {
  1051.       /* can't meet stencil buffer requirements */
  1052.       return NULL;
  1053.    }
  1054.    if (accumBits > (GLint) (8*sizeof(GLaccum))) {
  1055.       /* can't meet accum buffer requirements */
  1056.       return NULL;
  1057.    }
  1058.  
  1059.    vis = (GLvisual *) calloc( 1, sizeof(GLvisual) );
  1060.    if (!vis) {
  1061.       return NULL;
  1062.    }
  1063.  
  1064.    vis->RGBAflag   = rgbFlag;
  1065.    vis->DBflag     = dbFlag;
  1066.    vis->StereoFlag = stereoFlag;
  1067.    vis->RedBits    = redBits;
  1068.    vis->GreenBits  = greenBits;
  1069.    vis->BlueBits   = blueBits;
  1070.    vis->AlphaBits  = alphaFlag ? 8*sizeof(GLubyte) : alphaBits;
  1071.  
  1072.    vis->IndexBits   = indexBits;
  1073.    vis->DepthBits   = (depthBits>0) ? 8*sizeof(GLdepth) : 0;
  1074.    vis->AccumBits   = (accumBits>0) ? 8*sizeof(GLaccum) : 0;
  1075.    vis->StencilBits = (stencilBits>0) ? 8*sizeof(GLstencil) : 0;
  1076.  
  1077.    /* software alpha buffers */
  1078.    if (alphaFlag) {
  1079.       vis->FrontAlphaEnabled = GL_TRUE;
  1080.       if (dbFlag) {
  1081.      vis->BackAlphaEnabled = GL_TRUE;
  1082.       }
  1083.    }
  1084.  
  1085.    return vis;
  1086. }
  1087.  
  1088.  
  1089.  
  1090.  
  1091. void gl_destroy_visual( GLvisual *vis )
  1092. {
  1093.    free( vis );
  1094. }
  1095.  
  1096.  
  1097.  
  1098. /*
  1099.  * Allocate the proxy textures.  If we run out of memory part way through
  1100.  * the allocations clean up and return GL_FALSE.
  1101.  * Return:  GL_TRUE=success, GL_FALSE=failure
  1102.  */
  1103. static GLboolean alloc_proxy_textures( GLcontext *ctx )
  1104. {
  1105.    GLboolean out_of_memory;
  1106.    GLint i;
  1107.  
  1108.    ctx->Texture.Proxy1D = gl_alloc_texture_object(NULL, 0, 1);
  1109.    if (!ctx->Texture.Proxy1D) {
  1110.       return GL_FALSE;
  1111.    }
  1112.  
  1113.    ctx->Texture.Proxy2D = gl_alloc_texture_object(NULL, 0, 2);
  1114.    if (!ctx->Texture.Proxy2D) {
  1115.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1116.       return GL_FALSE;
  1117.    }
  1118.  
  1119.    ctx->Texture.Proxy3D = gl_alloc_texture_object(NULL, 0, 3);
  1120.    if (!ctx->Texture.Proxy3D) {
  1121.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1122.       gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1123.       return GL_FALSE;
  1124.    }
  1125.  
  1126.    out_of_memory = GL_FALSE;
  1127.    for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1128.       ctx->Texture.Proxy1D->Image[i] = gl_alloc_texture_image();
  1129.       ctx->Texture.Proxy2D->Image[i] = gl_alloc_texture_image();
  1130.       ctx->Texture.Proxy3D->Image[i] = gl_alloc_texture_image();
  1131.       if (!ctx->Texture.Proxy1D->Image[i]
  1132.       || !ctx->Texture.Proxy2D->Image[i]
  1133.       || !ctx->Texture.Proxy3D->Image[i]) {
  1134.      out_of_memory = GL_TRUE;
  1135.       }
  1136.    }
  1137.    if (out_of_memory) {
  1138.       for (i=0;i<MAX_TEXTURE_LEVELS;i++) {
  1139.      if (ctx->Texture.Proxy1D->Image[i]) {
  1140.         gl_free_texture_image(ctx->Texture.Proxy1D->Image[i]);
  1141.      }
  1142.      if (ctx->Texture.Proxy2D->Image[i]) {
  1143.         gl_free_texture_image(ctx->Texture.Proxy2D->Image[i]);
  1144.      }
  1145.      if (ctx->Texture.Proxy3D->Image[i]) {
  1146.         gl_free_texture_image(ctx->Texture.Proxy3D->Image[i]);
  1147.      }
  1148.       }
  1149.       gl_free_texture_object(NULL, ctx->Texture.Proxy1D);
  1150.       gl_free_texture_object(NULL, ctx->Texture.Proxy2D);
  1151.       gl_free_texture_object(NULL, ctx->Texture.Proxy3D);
  1152.       return GL_FALSE;
  1153.    }
  1154.    else {
  1155.       return GL_TRUE;
  1156.    }
  1157. }
  1158.  
  1159.  
  1160.  
  1161. /*
  1162.  * Allocate and initialize a GLcontext structure.
  1163.  * Input:  visual - a GLvisual pointer
  1164.  *         sharelist - another context to share display lists with or NULL
  1165.  *         driver_ctx - pointer to device driver's context state struct
  1166.  * Return:  pointer to a new gl_context struct or NULL if error.
  1167.  */
  1168. GLcontext *gl_create_context( GLvisual *visual,
  1169.                   GLcontext *share_list,
  1170.                   void *driver_ctx,
  1171.                   GLboolean direct )
  1172. {
  1173.    GLcontext *ctx;
  1174.  
  1175.    /* do some implementation tests */
  1176.    assert( sizeof(GLbyte) == 1 );
  1177.    assert( sizeof(GLshort) >= 2 );
  1178.    assert( sizeof(GLint) >= 4 );
  1179.    assert( sizeof(GLubyte) == 1 );
  1180.    assert( sizeof(GLushort) >= 2 );
  1181.    assert( sizeof(GLuint) >= 4 );
  1182.  
  1183.    /* misc one-time initializations */
  1184.    gl_init_math();
  1185.    gl_init_lists();
  1186.    gl_init_eval();
  1187.  
  1188.    ctx = (GLcontext *) calloc( 1, sizeof(GLcontext) );
  1189.    if (!ctx) {
  1190.       return NULL;
  1191.    }
  1192.  
  1193.    ctx->DriverCtx = driver_ctx;
  1194.    ctx->Visual = visual;
  1195.    ctx->Buffer = NULL;
  1196.  
  1197.    ctx->VB = gl_alloc_vb();
  1198.    if (!ctx->VB) {
  1199.       free( ctx );
  1200.       return NULL;
  1201.    }
  1202.  
  1203.    ctx->PB = gl_alloc_pb();
  1204.    if (!ctx->PB) {
  1205.       free( ctx->VB );
  1206.       free( ctx );
  1207.       return NULL;
  1208.    }
  1209.  
  1210.    if (share_list) {
  1211.       /* share the group of display lists of another context */
  1212.       ctx->Shared = share_list->Shared;
  1213.    }
  1214.    else {
  1215.       /* allocate new group of display lists */
  1216.       ctx->Shared = alloc_shared_state();
  1217.       if (!ctx->Shared) {
  1218.      free(ctx->VB);
  1219.      free(ctx->PB);
  1220.      free(ctx);
  1221.      return NULL;
  1222.       }
  1223.    }
  1224.    ctx->Shared->RefCount++;
  1225.  
  1226.    initialize_context( ctx );
  1227.  
  1228.    ctx->DirectContext = direct;
  1229.  
  1230.    if (visual->DBflag) {
  1231.       ctx->Color.DrawBuffer = GL_BACK;
  1232.       ctx->Pixel.ReadBuffer = GL_BACK;
  1233.    }
  1234.    else {
  1235.       ctx->Color.DrawBuffer = GL_FRONT;
  1236.       ctx->Pixel.ReadBuffer = GL_FRONT;
  1237.    }
  1238.  
  1239. #ifdef PROFILE
  1240.    init_timings( ctx );
  1241. #endif
  1242.  
  1243. #ifdef GL_VERSION_1_1
  1244.    if (!alloc_proxy_textures(ctx)) {
  1245.       free_shared_state(ctx, ctx->Shared);
  1246.       free(ctx->VB);
  1247.       free(ctx->PB);
  1248.       free(ctx);
  1249.       return NULL;
  1250.    }
  1251. #endif
  1252.  
  1253.    gl_init_api_function_pointers( ctx );
  1254.    ctx->API = ctx->Exec;   /* GL_EXECUTE is default */
  1255.  
  1256.    return ctx;
  1257. }
  1258.  
  1259.  
  1260.  
  1261. /*
  1262.  * Destroy a gl_context structure.
  1263.  */
  1264. void gl_destroy_context( GLcontext *ctx )
  1265. {
  1266.    if (ctx) {
  1267.  
  1268. #ifdef PROFILE
  1269.       if (getenv("MESA_PROFILE")) {
  1270.      print_timings( ctx );
  1271.       }
  1272. #endif
  1273.  
  1274.       free( ctx->PB );
  1275.       free( ctx->VB );
  1276.  
  1277.       ctx->Shared->RefCount--;
  1278.       assert(ctx->Shared->RefCount>=0);
  1279.       if (ctx->Shared->RefCount==0) {
  1280.      /* free shared state */
  1281.      free_shared_state( ctx, ctx->Shared );
  1282.       }
  1283.  
  1284.       /* Free proxy texture objects */
  1285.       gl_free_texture_object( NULL, ctx->Texture.Proxy1D );
  1286.       gl_free_texture_object( NULL, ctx->Texture.Proxy2D );
  1287.       gl_free_texture_object( NULL, ctx->Texture.Proxy3D );
  1288.  
  1289.       /* Free evaluator data */
  1290.       if (ctx->EvalMap.Map1Vertex3.Points)
  1291.      free( ctx->EvalMap.Map1Vertex3.Points );
  1292.       if (ctx->EvalMap.Map1Vertex4.Points)
  1293.      free( ctx->EvalMap.Map1Vertex4.Points );
  1294.       if (ctx->EvalMap.Map1Index.Points)
  1295.      free( ctx->EvalMap.Map1Index.Points );
  1296.       if (ctx->EvalMap.Map1Color4.Points)
  1297.      free( ctx->EvalMap.Map1Color4.Points );
  1298.       if (ctx->EvalMap.Map1Normal.Points)
  1299.      free( ctx->EvalMap.Map1Normal.Points );
  1300.       if (ctx->EvalMap.Map1Texture1.Points)
  1301.      free( ctx->EvalMap.Map1Texture1.Points );
  1302.       if (ctx->EvalMap.Map1Texture2.Points)
  1303.      free( ctx->EvalMap.Map1Texture2.Points );
  1304.       if (ctx->EvalMap.Map1Texture3.Points)
  1305.      free( ctx->EvalMap.Map1Texture3.Points );
  1306.       if (ctx->EvalMap.Map1Texture4.Points)
  1307.      free( ctx->EvalMap.Map1Texture4.Points );
  1308.  
  1309.       if (ctx->EvalMap.Map2Vertex3.Points)
  1310.      free( ctx->EvalMap.Map2Vertex3.Points );
  1311.       if (ctx->EvalMap.Map2Vertex4.Points)
  1312.      free( ctx->EvalMap.Map2Vertex4.Points );
  1313.       if (ctx->EvalMap.Map2Index.Points)
  1314.      free( ctx->EvalMap.Map2Index.Points );
  1315.       if (ctx->EvalMap.Map2Color4.Points)
  1316.      free( ctx->EvalMap.Map2Color4.Points );
  1317.       if (ctx->EvalMap.Map2Normal.Points)
  1318.      free( ctx->EvalMap.Map2Normal.Points );
  1319.       if (ctx->EvalMap.Map2Texture1.Points)
  1320.      free( ctx->EvalMap.Map2Texture1.Points );
  1321.       if (ctx->EvalMap.Map2Texture2.Points)
  1322.      free( ctx->EvalMap.Map2Texture2.Points );
  1323.       if (ctx->EvalMap.Map2Texture3.Points)
  1324.      free( ctx->EvalMap.Map2Texture3.Points );
  1325.       if (ctx->EvalMap.Map2Texture4.Points)
  1326.      free( ctx->EvalMap.Map2Texture4.Points );
  1327.  
  1328.       free( (void *) ctx );
  1329.  
  1330. #ifndef THREADS
  1331.       if (ctx==CC) {
  1332.      CC = NULL;
  1333.       }
  1334. #endif
  1335.  
  1336.    }
  1337. }
  1338.  
  1339.  
  1340.  
  1341. /*
  1342.  * Create a new framebuffer.  A GLframebuffer is a struct which
  1343.  * encapsulates the depth, stencil and accum buffers and related
  1344.  * parameters.
  1345.  * Input:  visual - a GLvisual pointer
  1346.  * Return:  pointer to new GLframebuffer struct or NULL if error.
  1347.  */
  1348. GLframebuffer *gl_create_framebuffer( GLvisual *visual )
  1349. {
  1350.    GLframebuffer *buffer;
  1351.  
  1352.    buffer = (GLframebuffer *) calloc( 1, sizeof(GLframebuffer) );
  1353.    if (!buffer) {
  1354.       return NULL;
  1355.    }
  1356.  
  1357.    buffer->Visual = visual;
  1358.  
  1359.    return buffer;
  1360. }
  1361.  
  1362.  
  1363.  
  1364. /*
  1365.  * Free a framebuffer struct and its buffers.
  1366.  */
  1367. void gl_destroy_framebuffer( GLframebuffer *buffer )
  1368. {
  1369.    if (buffer) {
  1370.       if (buffer->Depth) {
  1371.      free( buffer->Depth );
  1372.       }
  1373.       if (buffer->Accum) {
  1374.      free( buffer->Accum );
  1375.       }
  1376.       if (buffer->Stencil) {
  1377.      free( buffer->Stencil );
  1378.       }
  1379.       if (buffer->FrontAlpha) {
  1380.      free( buffer->FrontAlpha );
  1381.       }
  1382.       if (buffer->BackAlpha) {
  1383.      free( buffer->BackAlpha );
  1384.       }
  1385.       free(buffer);
  1386.    }
  1387. }
  1388.  
  1389.  
  1390.  
  1391. /*
  1392.  * Set the current context, binding the given frame buffer to the context.
  1393.  */
  1394. void gl_make_current( GLcontext *ctx, GLframebuffer *buffer )
  1395. {
  1396. #ifdef THREADS
  1397.    /* TODO: unbind old buffer from context? */
  1398.    set_thread_context( ctx );
  1399. #else
  1400.    if (CC && CC->Buffer) {
  1401.       /* unbind frame buffer from context */
  1402.       CC->Buffer = NULL;
  1403.    }
  1404.    CC = ctx;
  1405. #endif
  1406.  
  1407.    if (ctx && buffer) {
  1408.       /* TODO: check if ctx and buffer's visual match??? */
  1409.       ctx->Buffer = buffer;      /* Bind the frame buffer to the context */
  1410.       ctx->NewState = NEW_ALL;   /* just to be safe */
  1411.       gl_update_state( ctx );
  1412.    }
  1413. }
  1414.  
  1415.  
  1416. /*
  1417.  * Return current context handle.
  1418.  */
  1419. GLcontext *gl_get_current_context( void )
  1420. {
  1421. #ifdef THREADS
  1422.    return gl_get_thread_context();
  1423. #else
  1424.    return CC;
  1425. #endif
  1426. }
  1427.  
  1428.  
  1429.  
  1430. /*
  1431.  * Copy attribute groups from one context to another.
  1432.  * Input:  src - source context
  1433.  *         dst - destination context
  1434.  *         mask - bitwise OR of GL_*_BIT flags
  1435.  */
  1436. void gl_copy_context( const GLcontext *src, GLcontext *dst, GLuint mask )
  1437. {
  1438.    if (mask & GL_ACCUM_BUFFER_BIT) {
  1439.       MEMCPY( &dst->Accum, &src->Accum, sizeof(struct gl_accum_attrib) );
  1440.    }
  1441.    if (mask & GL_COLOR_BUFFER_BIT) {
  1442.       MEMCPY( &dst->Color, &src->Color, sizeof(struct gl_colorbuffer_attrib) );
  1443.    }
  1444.    if (mask & GL_CURRENT_BIT) {
  1445.       MEMCPY( &dst->Current, &src->Current, sizeof(struct gl_current_attrib) );
  1446.    }
  1447.    if (mask & GL_DEPTH_BUFFER_BIT) {
  1448.       MEMCPY( &dst->Depth, &src->Depth, sizeof(struct gl_depthbuffer_attrib) );
  1449.    }
  1450.    if (mask & GL_ENABLE_BIT) {
  1451.       /* no op */
  1452.    }
  1453.    if (mask & GL_EVAL_BIT) {
  1454.       MEMCPY( &dst->Eval, &src->Eval, sizeof(struct gl_eval_attrib) );
  1455.    }
  1456.    if (mask & GL_FOG_BIT) {
  1457.       MEMCPY( &dst->Fog, &src->Fog, sizeof(struct gl_fog_attrib) );
  1458.    }
  1459.    if (mask & GL_HINT_BIT) {
  1460.       MEMCPY( &dst->Hint, &src->Hint, sizeof(struct gl_hint_attrib) );
  1461.    }
  1462.    if (mask & GL_LIGHTING_BIT) {
  1463.       MEMCPY( &dst->Light, &src->Light, sizeof(struct gl_light_attrib) );
  1464.    }
  1465.    if (mask & GL_LINE_BIT) {
  1466.       MEMCPY( &dst->Line, &src->Line, sizeof(struct gl_line_attrib) );
  1467.    }
  1468.    if (mask & GL_LIST_BIT) {
  1469.       MEMCPY( &dst->List, &src->List, sizeof(struct gl_list_attrib) );
  1470.    }
  1471.    if (mask & GL_PIXEL_MODE_BIT) {
  1472.       MEMCPY( &dst->Pixel, &src->Pixel, sizeof(struct gl_pixel_attrib) );
  1473.    }
  1474.    if (mask & GL_POINT_BIT) {
  1475.       MEMCPY( &dst->Point, &src->Point, sizeof(struct gl_point_attrib) );
  1476.    }
  1477.    if (mask & GL_POLYGON_BIT) {
  1478.       MEMCPY( &dst->Polygon, &src->Polygon, sizeof(struct gl_polygon_attrib) );
  1479.    }
  1480.    if (mask & GL_POLYGON_STIPPLE_BIT) {
  1481.       /* Use loop instead of MEMCPY due to problem with Portland Group's
  1482.        * C compiler.  Reported by John Stone.
  1483.        */
  1484.       int i;
  1485.       for (i=0;i<32;i++) {
  1486.      dst->PolygonStipple[i] = src->PolygonStipple[i];
  1487.       }
  1488.    }
  1489.    if (mask & GL_SCISSOR_BIT) {
  1490.       MEMCPY( &dst->Scissor, &src->Scissor, sizeof(struct gl_scissor_attrib) );
  1491.    }
  1492.    if (mask & GL_STENCIL_BUFFER_BIT) {
  1493.       MEMCPY( &dst->Stencil, &src->Stencil, sizeof(struct gl_stencil_attrib) );
  1494.    }
  1495.    if (mask & GL_TEXTURE_BIT) {
  1496.       MEMCPY( &dst->Texture, &src->Texture, sizeof(struct gl_texture_attrib) );
  1497.    }
  1498.    if (mask & GL_TRANSFORM_BIT) {
  1499.       MEMCPY( &dst->Transform, &src->Transform, sizeof(struct gl_transform_attrib) );
  1500.    }
  1501.    if (mask & GL_VIEWPORT_BIT) {
  1502.       MEMCPY( &dst->Viewport, &src->Viewport, sizeof(struct gl_viewport_attrib) );
  1503.    }
  1504. }
  1505.  
  1506.  
  1507.  
  1508. /*
  1509.  * Someday a GLS library or OpenGL-like debugger may call this function
  1510.  * to register it's own set of API entry points.
  1511.  * Input: ctx - the context to set API pointers for
  1512.  *        api - if NULL, restore original API pointers
  1513.  *              else, set API function table to this table.
  1514.  */
  1515. void gl_set_api_table( GLcontext *ctx, const struct gl_api_table *api )
  1516. {
  1517.    if (api) {
  1518.       MEMCPY( &ctx->API, api, sizeof(struct gl_api_table) );
  1519.    }
  1520.    else {
  1521.       MEMCPY( &ctx->API, &ctx->Exec, sizeof(struct gl_api_table) );
  1522.    }
  1523. }
  1524.  
  1525.  
  1526.  
  1527.  
  1528. /**********************************************************************/
  1529. /*****                Miscellaneous functions                     *****/
  1530. /**********************************************************************/
  1531.  
  1532.  
  1533. /*
  1534.  * This function is called when the Mesa user has stumbled into a code
  1535.  * path which may not be implemented fully or correctly.
  1536.  */
  1537. void gl_problem( const GLcontext *ctx, const char *s )
  1538. {
  1539.    fprintf( stderr, "Mesa implementation error: %s\n", s );
  1540.    fprintf( stderr, "Report to Mesa author.\n" );
  1541.    (void) ctx;
  1542. }
  1543.  
  1544.  
  1545.  
  1546. /*
  1547.  * This is called to inform the user that he or she has tried to do
  1548.  * something illogical or if there's likely a bug in their program
  1549.  * (like enabled depth testing without a depth buffer).
  1550.  */
  1551. void gl_warning( const GLcontext *ctx, const char *s )
  1552. {
  1553.    GLboolean debug;
  1554. #ifdef DEBUG
  1555.    debug = GL_TRUE;
  1556. #else
  1557.    if (getenv("MESA_DEBUG")) {
  1558.       debug = GL_TRUE;
  1559.    }
  1560.    else {
  1561.       debug = GL_FALSE;
  1562.    }
  1563. #endif
  1564.    if (debug) {
  1565.       fprintf( stderr, "Mesa warning: %s\n", s );
  1566.    }
  1567.    (void) ctx;
  1568. }
  1569.  
  1570.  
  1571.  
  1572. /*
  1573.  * This is Mesa's error handler.  Normally, all that's done is the updating
  1574.  * of the current error value.  If Mesa is compiled with -DDEBUG or if the
  1575.  * environment variable "MESA_DEBUG" is defined then a real error message
  1576.  * is printed to stderr.
  1577.  * Input:  error - the error value
  1578.  *         s - a diagnostic string
  1579.  */
  1580. void gl_error( GLcontext *ctx, GLenum error, const char *s )
  1581. {
  1582.    GLboolean debug;
  1583.  
  1584. #ifdef DEBUG
  1585.    debug = GL_TRUE;
  1586. #else
  1587.    if (getenv("MESA_DEBUG")) {
  1588.       debug = GL_TRUE;
  1589.    }
  1590.    else {
  1591.       debug = GL_FALSE;
  1592.    }
  1593. #endif
  1594.  
  1595.    if (debug) {
  1596.       char errstr[1000];
  1597.  
  1598.       switch (error) {
  1599.      case GL_NO_ERROR:
  1600.         strcpy( errstr, "GL_NO_ERROR" );
  1601.         break;
  1602.      case GL_INVALID_VALUE:
  1603.         strcpy( errstr, "GL_INVALID_VALUE" );
  1604.         break;
  1605.      case GL_INVALID_ENUM:
  1606.         strcpy( errstr, "GL_INVALID_ENUM" );
  1607.         break;
  1608.      case GL_INVALID_OPERATION:
  1609.         strcpy( errstr, "GL_INVALID_OPERATION" );
  1610.         break;
  1611.      case GL_STACK_OVERFLOW:
  1612.         strcpy( errstr, "GL_STACK_OVERFLOW" );
  1613.         break;
  1614.      case GL_STACK_UNDERFLOW:
  1615.         strcpy( errstr, "GL_STACK_UNDERFLOW" );
  1616.         break;
  1617.      case GL_OUT_OF_MEMORY:
  1618.         strcpy( errstr, "GL_OUT_OF_MEMORY" );
  1619.         break;
  1620.      default:
  1621.         strcpy( errstr, "unknown" );
  1622.         break;
  1623.       }
  1624.       fprintf( stderr, "Mesa user error: %s in %s\n", errstr, s );
  1625.    }
  1626.  
  1627.    if (ctx->ErrorValue==GL_NO_ERROR) {
  1628.       ctx->ErrorValue = error;
  1629.    }
  1630.  
  1631.    /* Call device driver's error handler, if any.  This is used on the Mac. */
  1632.    if (ctx->Driver.Error) {
  1633.       (*ctx->Driver.Error)( ctx );
  1634.    }
  1635. }
  1636.  
  1637.  
  1638.  
  1639.  
  1640. GLenum gl_GetError( GLcontext *ctx )
  1641. {
  1642.    GLenum e;
  1643.  
  1644.    if (INSIDE_BEGIN_END(ctx)) {
  1645.       gl_error( ctx, GL_INVALID_OPERATION, "glGetError" );
  1646.       return GL_INVALID_OPERATION;
  1647.    }
  1648.  
  1649.    e = ctx->ErrorValue;
  1650.    ctx->ErrorValue = (GLenum) GL_NO_ERROR;
  1651.    return e;
  1652. }
  1653.  
  1654.  
  1655.  
  1656. void gl_ResizeBuffersMESA( GLcontext *ctx )
  1657. {
  1658.    GLint newsize;
  1659.    GLuint buf_width, buf_height;
  1660.    
  1661.    ctx->NewState |= NEW_ALL;   /* just to be safe */
  1662.    /* ask device driver for size of output buffer */
  1663.    (*ctx->Driver.GetBufferSize)( ctx, &buf_width, &buf_height );
  1664.  
  1665.    /* see if size of device driver's color buffer (window) has changed */
  1666.    newsize = ctx->Buffer->Width != (GLint) buf_width
  1667.       || ctx->Buffer->Height != (GLint) buf_height;
  1668.  
  1669.    /* save buffer size */
  1670.    ctx->Buffer->Width = buf_width;
  1671.    ctx->Buffer->Height = buf_height;
  1672.  
  1673.    /* Reallocate other buffers if needed. */
  1674.    if (newsize && ctx->Visual->DepthBits>0) {
  1675.       /* reallocate depth buffer */
  1676.       (*ctx->Driver.AllocDepthBuffer)( ctx );
  1677.    }
  1678.    if (newsize && ctx->Visual->StencilBits>0) {
  1679.       /* reallocate stencil buffer */
  1680.       gl_alloc_stencil_buffer( ctx );
  1681.    }
  1682.    if (newsize && ctx->Visual->AccumBits>0) {
  1683.       /* reallocate accum buffer */
  1684.       gl_alloc_accum_buffer( ctx );
  1685.    }
  1686.    if (newsize
  1687.        && (ctx->Visual->FrontAlphaEnabled || ctx->Visual->BackAlphaEnabled)) {
  1688.       gl_alloc_alpha_buffers( ctx );
  1689.    }
  1690. }
  1691.  
  1692.  
  1693.  
  1694.  
  1695. /**********************************************************************/
  1696. /*****                   State update logic                       *****/
  1697. /**********************************************************************/
  1698.  
  1699.  
  1700. /*
  1701.  * Since the device driver may or may not support pixel logic ops we
  1702.  * have to make some extensive tests to determine whether or not
  1703.  * software-implemented logic operations have to be used.
  1704.  */
  1705. static void update_pixel_logic( GLcontext *ctx )
  1706. {
  1707.    if (ctx->Visual->RGBAflag) {
  1708.       /* RGBA mode blending w/ Logic Op */
  1709.       if (ctx->Color.ColorLogicOpEnabled) {
  1710.      if (ctx->Driver.LogicOp
  1711.          && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
  1712.         /* Device driver can do logic, don't have to do it in software */
  1713.         ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1714.      }
  1715.      else {
  1716.         /* Device driver can't do logic op so we do it in software */
  1717.         ctx->Color.SWLogicOpEnabled = GL_TRUE;
  1718.      }
  1719.       }
  1720.       else {
  1721.      /* no logic op */
  1722.      if (ctx->Driver.LogicOp) {
  1723.         (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
  1724.      }
  1725.      ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1726.       }
  1727.    }
  1728.    else {
  1729.       /* CI mode Logic Op */
  1730.       if (ctx->Color.IndexLogicOpEnabled) {
  1731.      if (ctx->Driver.LogicOp
  1732.          && (*ctx->Driver.LogicOp)( ctx, ctx->Color.LogicOp )) {
  1733.         /* Device driver can do logic, don't have to do it in software */
  1734.         ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1735.      }
  1736.      else {
  1737.         /* Device driver can't do logic op so we do it in software */
  1738.         ctx->Color.SWLogicOpEnabled = GL_TRUE;
  1739.      }
  1740.       }
  1741.       else {
  1742.      /* no logic op */
  1743.      if (ctx->Driver.LogicOp) {
  1744.         (void) (*ctx->Driver.LogicOp)( ctx, GL_COPY );
  1745.      }
  1746.      ctx->Color.SWLogicOpEnabled = GL_FALSE;
  1747.       }
  1748.    }
  1749. }
  1750.  
  1751.  
  1752.  
  1753. /*
  1754.  * Check if software implemented RGBA or Color Index masking is needed.
  1755.  */
  1756. static void update_pixel_masking( GLcontext *ctx )
  1757. {
  1758.    if (ctx->Visual->RGBAflag) {
  1759.       GLuint *colorMask = (GLuint *) ctx->Color.ColorMask;
  1760.       if (*colorMask == 0xffffffff) {
  1761.      /* disable masking */
  1762.      if (ctx->Driver.ColorMask) {
  1763.         (void) (*ctx->Driver.ColorMask)( ctx, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  1764.      }
  1765.      ctx->Color.SWmasking = GL_FALSE;
  1766.       }
  1767.       else {
  1768.      /* Ask driver to do color masking, if it can't then
  1769.       * do it in software
  1770.       */
  1771.      GLboolean red   = ctx->Color.ColorMask[RCOMP] ? GL_TRUE : GL_FALSE;
  1772.      GLboolean green = ctx->Color.ColorMask[GCOMP] ? GL_TRUE : GL_FALSE;
  1773.      GLboolean blue  = ctx->Color.ColorMask[BCOMP] ? GL_TRUE : GL_FALSE;
  1774.      GLboolean alpha = ctx->Color.ColorMask[ACOMP] ? GL_TRUE : GL_FALSE;
  1775.      if (ctx->Driver.ColorMask
  1776.          && (*ctx->Driver.ColorMask)( ctx, red, green, blue, alpha )) {
  1777.         ctx->Color.SWmasking = GL_FALSE;
  1778.      }
  1779.      else {
  1780.         ctx->Color.SWmasking = GL_TRUE;
  1781.      }
  1782.       }
  1783.    }
  1784.    else {
  1785.       if (ctx->Color.IndexMask==0xffffffff) {
  1786.      /* disable masking */
  1787.      if (ctx->Driver.IndexMask) {
  1788.         (void) (*ctx->Driver.IndexMask)( ctx, 0xffffffff );
  1789.      }
  1790.      ctx->Color.SWmasking = GL_FALSE;
  1791.       }
  1792.       else {
  1793.      /* Ask driver to do index masking, if it can't then
  1794.       * do it in software
  1795.       */
  1796.      if (ctx->Driver.IndexMask
  1797.          && (*ctx->Driver.IndexMask)( ctx, ctx->Color.IndexMask )) {
  1798.         ctx->Color.SWmasking = GL_FALSE;
  1799.      }
  1800.      else {
  1801.         ctx->Color.SWmasking = GL_TRUE;
  1802.      }
  1803.       }
  1804.    }
  1805. }
  1806.  
  1807.  
  1808. static void update_fog_mode( GLcontext *ctx )
  1809. {
  1810.    if (ctx->Fog.Enabled) {
  1811.       if (ctx->Texture.Enabled)
  1812.      ctx->FogMode = FOG_FRAGMENT;
  1813.       else if (ctx->Hint.Fog == GL_NICEST)
  1814.      ctx->FogMode = FOG_FRAGMENT;
  1815.       else
  1816.      ctx->FogMode = FOG_VERTEX;
  1817.  
  1818.       if (ctx->Driver.GetParameteri)
  1819.      if ((ctx->Driver.GetParameteri)( ctx, DD_HAVE_HARDWARE_FOG ))
  1820.         ctx->FogMode = FOG_FRAGMENT;
  1821.    }
  1822.    else {
  1823.       ctx->FogMode = FOG_NONE;
  1824.    }
  1825. }
  1826.  
  1827.  
  1828. /*
  1829.  * Recompute the value of ctx->RasterMask, etc. according to
  1830.  * the current context.
  1831.  */
  1832. static void update_rasterflags( GLcontext *ctx )
  1833. {
  1834.    ctx->RasterMask = 0;
  1835.  
  1836.    if (ctx->Color.AlphaEnabled)         ctx->RasterMask |= ALPHATEST_BIT;
  1837.    if (ctx->Color.BlendEnabled)         ctx->RasterMask |= BLEND_BIT;
  1838.    if (ctx->Depth.Test)                 ctx->RasterMask |= DEPTH_BIT;
  1839.    if (ctx->FogMode==FOG_FRAGMENT)      ctx->RasterMask |= FOG_BIT;
  1840.    if (ctx->Color.SWLogicOpEnabled)     ctx->RasterMask |= LOGIC_OP_BIT;
  1841.    if (ctx->Scissor.Enabled)            ctx->RasterMask |= SCISSOR_BIT;
  1842.    if (ctx->Stencil.Enabled)            ctx->RasterMask |= STENCIL_BIT;
  1843.    if (ctx->Color.SWmasking)            ctx->RasterMask |= MASKING_BIT;
  1844.    if (ctx->Visual->FrontAlphaEnabled)  ctx->RasterMask |= ALPHABUF_BIT;
  1845.    if (ctx->Visual->BackAlphaEnabled)   ctx->RasterMask |= ALPHABUF_BIT;
  1846.  
  1847.    if (   ctx->Viewport.X<0
  1848.        || ctx->Viewport.X + ctx->Viewport.Width > ctx->Buffer->Width
  1849.        || ctx->Viewport.Y<0
  1850.        || ctx->Viewport.Y + ctx->Viewport.Height > ctx->Buffer->Height) {
  1851.       ctx->RasterMask |= WINCLIP_BIT;
  1852.    }
  1853.  
  1854.    /* check if drawing to front and back buffers */
  1855.    if (ctx->Color.DrawBuffer==GL_FRONT_AND_BACK) {
  1856.       ctx->RasterMask |= FRONT_AND_BACK_BIT;
  1857.    }
  1858.  
  1859.    /* check if writing to color buffer(s) is disabled */
  1860.    if (ctx->Color.DrawBuffer==GL_NONE) {
  1861.       ctx->RasterMask |= NO_DRAW_BIT;
  1862.    }
  1863.    else if (ctx->Visual->RGBAflag && ctx->Color.ColorMask==0) {
  1864.       ctx->RasterMask |= NO_DRAW_BIT;
  1865.    }
  1866.    else if (!ctx->Visual->RGBAflag && ctx->Color.IndexMask==0) {
  1867.       ctx->RasterMask |= NO_DRAW_BIT;
  1868.    }
  1869. }
  1870.  
  1871.  
  1872. /*
  1873.  * Recompute the value of ctx->ClipMask according to the current context.
  1874.  * ClipMask depends on Texturing and Lighting.
  1875.  */
  1876. static void update_clipmask(GLcontext *ctx)
  1877. {
  1878.    /* Recompute ClipMask (what has to be interpolated when clipping) */
  1879.    ctx->ClipMask = 0;
  1880.    if (ctx->Texture.Enabled) {
  1881.       ctx->ClipMask |= CLIP_TEXTURE_BIT;
  1882.    }
  1883.    if (ctx->Light.ShadeModel==GL_SMOOTH) {
  1884.       if (ctx->Visual->RGBAflag) {
  1885.      ctx->ClipMask |= CLIP_FCOLOR_BIT;
  1886.      if (ctx->Light.Model.TwoSide) {
  1887.         ctx->ClipMask |= CLIP_BCOLOR_BIT;
  1888.      }
  1889.       }
  1890.       else {
  1891.      ctx->ClipMask |= CLIP_FINDEX_BIT;
  1892.      if (ctx->Light.Model.TwoSide) {
  1893.         ctx->ClipMask |= CLIP_BINDEX_BIT;
  1894.      }
  1895.       }
  1896.    }
  1897.  
  1898.    if (ctx->Texture.Enabled >= TEXTURE1_1D) {
  1899.       /* Multi texture coords */
  1900.       ctx->ClipInterpFunc = gl_clip_interp_all;
  1901.    }
  1902.    else {
  1903.       switch(ctx->ClipMask) {
  1904.      case CLIP_FCOLOR_BIT | CLIP_TEXTURE_BIT:
  1905.         ctx->ClipInterpFunc = gl_clip_interp_color_tex;
  1906.         break;
  1907.      case CLIP_TEXTURE_BIT:
  1908.         ctx->ClipInterpFunc = gl_clip_interp_tex;
  1909.         break;
  1910.      case CLIP_FCOLOR_BIT:
  1911.         ctx->ClipInterpFunc = gl_clip_interp_color;
  1912.         break;
  1913.      default:
  1914.         ctx->ClipInterpFunc = gl_clip_interp_all;
  1915.       }
  1916.    }
  1917. }
  1918.  
  1919.  
  1920.  
  1921. /*
  1922.  * If ctx->NewState is non-zero then this function MUST be called before
  1923.  * rendering any primitive.  Basically, function pointers and miscellaneous
  1924.  * flags are updated to reflect the current state of the state machine.
  1925.  */
  1926. void gl_update_state( GLcontext *ctx )
  1927. {
  1928.    if (ctx->NewState & NEW_RASTER_OPS) {
  1929.       update_pixel_logic(ctx);
  1930.       update_pixel_masking(ctx);
  1931.       update_fog_mode(ctx);
  1932.       update_rasterflags(ctx);
  1933.       if (ctx->Driver.Dither) {
  1934.      (*ctx->Driver.Dither)( ctx, ctx->Color.DitherFlag );
  1935.       }
  1936.    }
  1937.  
  1938.    if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING)) {
  1939.       update_clipmask(ctx);
  1940.    }
  1941.  
  1942.    if (ctx->NewState & NEW_LIGHTING) {
  1943.       gl_update_lighting(ctx);
  1944.       gl_set_color_function(ctx);
  1945.    }
  1946.  
  1947.    if (ctx->NewState & NEW_TEXTURING) {
  1948.       gl_update_texture_state(ctx);
  1949.    }
  1950.  
  1951.    if (ctx->NewState & (NEW_LIGHTING | NEW_TEXTURING)) {
  1952.       /* Check if normal vectors are needed */
  1953.       GLboolean sphereGen = GL_FALSE;
  1954.       if (ctx->Texture.Enabled) {
  1955.      GLuint texSet;
  1956.      for (texSet=0; texSet<MAX_TEX_SETS; texSet++) {
  1957.         if ((ctx->Texture.Set[texSet].GenModeS==GL_SPHERE_MAP
  1958.          && (ctx->Texture.Set[texSet].TexGenEnabled & S_BIT))
  1959.         || (ctx->Texture.Set[texSet].GenModeT==GL_SPHERE_MAP
  1960.             && (ctx->Texture.Set[texSet].TexGenEnabled & T_BIT)))
  1961.            sphereGen = GL_TRUE;
  1962.      }
  1963.       }
  1964.       if (ctx->Light.Enabled || sphereGen) {
  1965.      ctx->NeedNormals = GL_TRUE;
  1966.       }
  1967.       else {
  1968.      ctx->NeedNormals = GL_FALSE;
  1969.       }
  1970.    }
  1971.  
  1972.    if (ctx->NewState & NEW_RASTER_OPS) {
  1973.       /* Check if incoming colors can be modified during rasterization */
  1974.       if (ctx->Fog.Enabled ||
  1975.       ctx->Texture.Enabled ||
  1976.       ctx->Color.BlendEnabled ||
  1977.       ctx->Color.SWmasking ||
  1978.       ctx->Color.SWLogicOpEnabled) {
  1979.      ctx->MutablePixels = GL_TRUE;
  1980.       }
  1981.       else {
  1982.      ctx->MutablePixels = GL_FALSE;
  1983.       }
  1984.    }
  1985.  
  1986.    if (ctx->NewState & (NEW_RASTER_OPS | NEW_LIGHTING)) {
  1987.       /* Check if all pixels generated are likely to be the same color */
  1988.       if (ctx->Light.ShadeModel==GL_SMOOTH ||
  1989.       ctx->Light.Enabled ||
  1990.       ctx->Fog.Enabled ||
  1991.       ctx->Texture.Enabled ||
  1992.       ctx->Color.BlendEnabled ||
  1993.       ctx->Color.SWmasking ||
  1994.       ctx->Color.SWLogicOpEnabled) {
  1995.      ctx->MonoPixels = GL_FALSE;       /* pixels probably multicolored */
  1996.       }
  1997.       else {
  1998.      /* pixels will all be same color,
  1999.       * only glColor() can invalidate this.
  2000.       */
  2001.      ctx->MonoPixels = GL_TRUE;
  2002.       }
  2003.    }
  2004.  
  2005.    if (ctx->NewState & NEW_POLYGON) {
  2006.       /* Setup CullBits bitmask */
  2007.       ctx->Polygon.CullBits = 0;
  2008.       if (ctx->Polygon.CullFlag) {
  2009.      if (ctx->Polygon.CullFaceMode==GL_FRONT ||
  2010.          ctx->Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  2011.         ctx->Polygon.CullBits |= 1;
  2012.      }
  2013.      if (ctx->Polygon.CullFaceMode==GL_BACK ||
  2014.          ctx->Polygon.CullFaceMode==GL_FRONT_AND_BACK) {
  2015.         ctx->Polygon.CullBits |= 2;
  2016.      }
  2017.       }
  2018.       /* Any Polygon offsets enabled? */
  2019.       ctx->Polygon.OffsetAny = ctx->Polygon.OffsetPoint ||
  2020.                    ctx->Polygon.OffsetLine ||
  2021.                    ctx->Polygon.OffsetFill;
  2022.       /* reset Z offsets now */
  2023.       ctx->PointZoffset   = 0.0;
  2024.       ctx->LineZoffset    = 0.0;
  2025.       ctx->PolygonZoffset = 0.0;
  2026.    }
  2027.  
  2028.    if (ctx->NewState & (NEW_POLYGON | NEW_LIGHTING)) {
  2029.       /* Determine if we can directly call the triangle rasterizer */
  2030.       if (   ctx->Polygon.Unfilled
  2031.       || ctx->Polygon.OffsetAny
  2032.       || ctx->Polygon.CullFlag
  2033.       || ctx->Light.Model.TwoSide
  2034.       || ctx->RenderMode!=GL_RENDER) {
  2035.      ctx->DirectTriangles = GL_FALSE;
  2036.       }
  2037.       else {
  2038.      ctx->DirectTriangles = GL_TRUE;
  2039.       }
  2040.    }
  2041.  
  2042.    /* update scissor region */
  2043.    ctx->Buffer->Xmin = 0;
  2044.    ctx->Buffer->Ymin = 0;
  2045.    ctx->Buffer->Xmax = ctx->Buffer->Width-1;
  2046.    ctx->Buffer->Ymax = ctx->Buffer->Height-1;
  2047.    if (ctx->Scissor.Enabled) {
  2048.       if (ctx->Scissor.X > ctx->Buffer->Xmin) {
  2049.      ctx->Buffer->Xmin = ctx->Scissor.X;
  2050.       }
  2051.       if (ctx->Scissor.Y > ctx->Buffer->Ymin) {
  2052.      ctx->Buffer->Ymin = ctx->Scissor.Y;
  2053.       }
  2054.       if (ctx->Scissor.X + ctx->Scissor.Width - 1 < ctx->Buffer->Xmax) {
  2055.      ctx->Buffer->Xmax = ctx->Scissor.X + ctx->Scissor.Width - 1;
  2056.       }
  2057.       if (ctx->Scissor.Y + ctx->Scissor.Height - 1 < ctx->Buffer->Ymax) {
  2058.      ctx->Buffer->Ymax = ctx->Scissor.Y + ctx->Scissor.Height - 1;
  2059.       }
  2060.    }
  2061.  
  2062.    /*
  2063.     * Update Device Driver interface
  2064.     */
  2065.    if (ctx->NewState & NEW_RASTER_OPS) {
  2066.       ctx->Driver.AllocDepthBuffer = gl_alloc_depth_buffer;
  2067.       if (ctx->Depth.Mask) {
  2068.      switch (ctx->Depth.Func) {
  2069.         case GL_LESS:
  2070.            ctx->Driver.DepthTestSpan = gl_depth_test_span_less;
  2071.            ctx->Driver.DepthTestPixels = gl_depth_test_pixels_less;
  2072.            break;
  2073.         case GL_GREATER:
  2074.            ctx->Driver.DepthTestSpan = gl_depth_test_span_greater;
  2075.            ctx->Driver.DepthTestPixels = gl_depth_test_pixels_greater;
  2076.            break;
  2077.         default:
  2078.            ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
  2079.            ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
  2080.      }
  2081.       }
  2082.       else {
  2083.      ctx->Driver.DepthTestSpan = gl_depth_test_span_generic;
  2084.      ctx->Driver.DepthTestPixels = gl_depth_test_pixels_generic;
  2085.       }
  2086.       ctx->Driver.ReadDepthSpanFloat = gl_read_depth_span_float;
  2087.       ctx->Driver.ReadDepthSpanInt = gl_read_depth_span_int;
  2088.    }
  2089.  
  2090.    ctx->Driver.PointsFunc = NULL;
  2091.    ctx->Driver.LineFunc = NULL;
  2092.    ctx->Driver.TriangleFunc = NULL;
  2093.    ctx->Driver.QuadFunc = NULL;
  2094.    ctx->Driver.RectFunc = NULL;
  2095. #ifdef DRIVEREXT
  2096.    ctx->Driver.LineStripFunc = NULL;
  2097.    ctx->Driver.TriStripFunc = NULL;
  2098.    ctx->Driver.TriFanFunc = NULL;
  2099. #endif
  2100.    /*
  2101.     * The ctx->Driver.UpdateState pointer _MUST_ be valid at this point
  2102.     * in order for any impending rendering to succeed.
  2103.     */
  2104.    assert(ctx->Driver.UpdateState);
  2105.  
  2106.    /*
  2107.     * Here the driver sets up all the ctx->Driver function pointers to
  2108.     * it's specific, private functions.
  2109.     */
  2110.    (*ctx->Driver.UpdateState)(ctx);
  2111.  
  2112.    /*
  2113.     * In case the driver didn't hook in an optimized point, line or
  2114.     * triangle function we'll now select "core/fallback" point, line
  2115.     * and triangle functions.
  2116.     */
  2117.    gl_set_point_function(ctx);
  2118.    gl_set_line_function(ctx);
  2119.    gl_set_triangle_function(ctx);
  2120.    gl_set_quad_function(ctx);
  2121.  
  2122.    gl_set_vertex_function(ctx);
  2123.  
  2124.    ctx->NewState = 0;
  2125. }
  2126.  
  2127.